package com.zhou.JavaAndroidFirm.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.activity.BigImagePagerActivity;
import com.zhou.JavaAndroidFirm.utils.ViewHolderUtil;
import com.zhou.JavaAndroidFirm.widget.imgsel.AndroidLifecycleUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2018/8/2 0002
 * @des 发表说说上传图片适配器（最多九张）
 */
public class NinePicturesAdapter extends BaseAdapter {
    private ArrayList<String> mData;
    private LayoutInflater inflater;
    private OnClickAddListener onClickAddListener;

    public void setOnClickAddListener(OnClickAddListener listener) {
        onClickAddListener = listener;
    }

    public void clearAll() {
        mData.clear();
    }

    public void addAll(List<String> photos) {
        if (photos != null) {
            mData.addAll(photos);
        }
        notifyDataSetChanged();
    }

    /**
     * 加号接口
     */
    public interface OnClickAddListener {
        void onClickAdd(int positin);
    }

    private Context mContext;

    final static int TYPE_ADD = 1;
    final static int TYPE_PHOTO = 2;

    final static int MAX = 9;

    public NinePicturesAdapter(Context mContext, ArrayList<String> photoPaths) {
        this.mData = photoPaths;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);

    }

    @Override
    public int getCount() {
        int count = mData.size() + 1;
        return count > MAX ? MAX : count;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public int getItemViewType(int position) {
        return (position == mData.size() && position != MAX) ? TYPE_ADD : TYPE_PHOTO;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grid_photo, parent, false);
        }
        final ImageView imageView = ViewHolderUtil.get(convertView, R.id.img_photo);
        ImageView imgDelete = ViewHolderUtil.get(convertView, R.id.img_delete);
        if (getItemViewType(position) == TYPE_ADD) {
            imgDelete.setVisibility(View.GONE);
            imageView.setImageResource(R.drawable.addphoto);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onClickAddListener != null) {
                        onClickAddListener.onClickAdd(position);
                    }
                }
            });
        } else {
            imgDelete.setVisibility(View.VISIBLE);
            Uri uri = Uri.fromFile(new File(mData.get(position)));
            boolean canLoadImage = AndroidLifecycleUtils.canLoadImage(mContext);

            if (canLoadImage) {
                final RequestOptions options = new RequestOptions();
                options.centerCrop()
                        .placeholder(R.drawable.ic_image_loading)
                        .error(R.drawable.ic_empty_picture);
                Glide.with(mContext)
                        .load(uri)
                        .apply(options)
                        .thumbnail(0.1f)
                        .into(imageView);
            }
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //放大查看图片
                    BigImagePagerActivity.startImagePagerActivity(mContext, mData, position);
                }
            });
            //删除按钮
            imgDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mData.remove(position);
                    notifyDataSetChanged();
                }
            });
        }


        return convertView;
    }

}